package com.gkd.controller.regular;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gkd.context.BaseContext;
import com.gkd.pojo.dto.FavoriteDTO;
import com.gkd.pojo.dto.FavoritePageQueryDTO;
import com.gkd.pojo.entity.Favorite;
import com.gkd.result.PageResult;
import com.gkd.result.Result;
import com.gkd.service.FavoriteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收藏相关接口
 */
@RestController("regularFavortiteController")
@CrossOrigin
@Slf4j
@RequestMapping("/regular/favorite")
public class FavortiteController {

    @Autowired
    private FavoriteService favoriteService;

    /**
     * 添加收藏
     * @param postId
     * @return
     */
    @PostMapping("/{postId}")
    public Result add(@PathVariable Integer postId){
        log.info("添加收藏:{}",postId);
        favoriteService.add(postId);
        return Result.success();
    }

    /**
     * 批量删除收藏
     * @param favoriteIds
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestBody List<Integer> favoriteIds){
        log.info("删除收藏:{}",favoriteIds);
        favoriteService.removeByIds(favoriteIds);
        return Result.success();
    }

    /**
     * 取消收藏
     * @param postId
     * @return
     */
    @DeleteMapping("/{postId}")
    public Result deleteByPostId(@PathVariable Integer postId){
        log.info("取消收藏:{}",postId);
        favoriteService.delete(postId);
        return Result.success();
    }

    /**
     * 通过用户id分页查询收藏
     * @param favoritePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(FavoritePageQueryDTO favoritePageQueryDTO){
        log.info("收藏分页查询:{}",favoritePageQueryDTO);
        favoritePageQueryDTO.setUserId(BaseContext.getCurrentId());
        PageResult pageResult=favoriteService.pageQuery(favoritePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过文章id和用户id获取收藏
     * @param postId
     * @return
     */
    @GetMapping("/{postId}")
    public Result<String> getByPostIdAndUserId(@PathVariable Integer postId){
        Integer userId = BaseContext.getCurrentId();
        log.info("获取收藏状态:postId={},userId={}",postId,userId);
        String status=favoriteService.getByPostIdAndUserId(postId,userId);
        return Result.success(status);
    }

    /**
     * 通过文章id获取收藏总数
     * @param postId
     * @return
     */
    @GetMapping("/count/{postId}")
    public Result<Long> count(@PathVariable Integer postId){
        log.info("获取通过文章id获取文章收藏总数:{}",postId);
        LambdaQueryWrapper<Favorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Favorite::getPostId,postId);
        Long count = favoriteService.count(wrapper);
        return Result.success(count);
    }



}
